package com.jrapider.utils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/18.
 */
public class TypeMapper {
    private static Map<String, Class> typeMap = new HashMap<>();

    static {
        typeMap.put("BIT", Boolean.class);
        typeMap.put("TINYINT", Integer.class);
        typeMap.put("BOOL", Boolean.class);
        typeMap.put("BOOLEAN", Boolean.class);
        typeMap.put("SMALLINT", Integer.class);
        typeMap.put("MEDIUMINT", Integer.class);
        typeMap.put("INT", Integer.class);
        typeMap.put("INTEGER", Integer.class);
        typeMap.put("BIGINT", Long.class);
        typeMap.put("LONG", Long.class);
        typeMap.put("DECIMAL", BigDecimal.class);
        typeMap.put("DEC", BigDecimal.class);
        typeMap.put("NUMERIC", Double.class);
        typeMap.put("FLOAT", Float.class);
        typeMap.put("DOUBLE", Double.class);
        typeMap.put("DOUBLE PRECISION", Double.class);
        typeMap.put("REAL", Double.class);
        typeMap.put("ENUM", Integer.class);
        // 日期型
        typeMap.put("DATE", Date.class);
        typeMap.put("TIME", Date.class);
        typeMap.put("DATETIME", Date.class);
        typeMap.put("TIMESTAMP", Date.class);
        typeMap.put("YEAR", Date.class);
        // 字符型
        typeMap.put("CHAR", String.class);
        typeMap.put("VARCHAR", String.class);
        typeMap.put("BINARY", byte[].class);
        typeMap.put("VARBINARY", byte[].class);
        typeMap.put("TINYBLOB", byte[].class);
        typeMap.put("BLOB", byte[].class);
        typeMap.put("MEDIUMBLOB", byte[].class);
        typeMap.put("LONGBLOB", byte[].class);
        typeMap.put("TEXT", String.class);
        typeMap.put("LONGTEXT", String.class);
    }


    public static String getJavaType(String dbType) {
        for (String key : typeMap.keySet()) {
            if (dbType.toUpperCase().startsWith(key)) {
                return typeMap.get(key).getName();
            }
        }
        return null;
    }
}
